package com.screwdriver.jso.processor;

import com.screwdriver.jso.processor.generator.MethodSignatureGenerator;
import com.screwdriver.jso.processor.utils.InterfaceVisitorAdapter;
import com.screwdriver.jso.processor.utils.PropertyUtil;
import com.screwdriver.jso.processor.utils.TypeUtil;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.declaration.InterfaceDeclaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.type.InterfaceType;

import java.io.PrintWriter;

@SuppressWarnings("deprecation")
public class MethodsGeneratingInterfaceVisitor extends InterfaceVisitorAdapter {

    private AnnotationProcessorEnvironment environment;
    private final PrintWriter sourceWriter;
    private MethodSignatureGenerator signatureComposer;
    private InterfaceDeclaration rootType;

    public MethodsGeneratingInterfaceVisitor(AnnotationProcessorEnvironment environment, PrintWriter sourceWriter) {
        this.environment = environment;
        this.sourceWriter = sourceWriter;
    }

    public void visitRoot(InterfaceDeclaration root) {
        this.rootType = root;
        this.signatureComposer = new MethodSignatureGenerator(environment);
    }

    @Override
    public void visitMethod(InterfaceType declaringSuperInterface, MethodDeclaration md) {
        if (!TypeUtil.isMethodImplementedByOtherJso(md, rootType)) {
            if (PropertyUtil.isPropertyMethod(md)) {
                sourceWriter.println(String.format("\t %s;", signatureComposer.generateMethodSignature(rootType, declaringSuperInterface, md)));
            } else {
                environment.getMessager().printWarning(String.format("%s is neither setter nor getter", md.toString()));
            }
        }
    }

}